<template>
	<view class="store-pro">
		<block v-if="show">
			<view class="store-pro-search">
				<u-search
					placeholder="搜索您想要的商品"
					:show-action="false"
					:disabled="true"
					@click="search"
					search-icon-color="#000000"
					placeholder-color="#989898">
				</u-search>
			</view>
			<view class="store-pro-class">
				<u-tabs
					:list="storeGoodsClassList"
					:current="storeGoodsClassIndex"
					@change="storeGoodsClassTabChange"
					active-color="#141414"
					inactive-color="#666666"
					name="class_name"
					font-size="28">
				</u-tabs>
			</view>
			<view class="store-pro-list">
				<pt-product-items
					:showSort="false"
					:object="{goods_list: storeGoodsList}"
					:isMerchant="true"
					:noMore="true"
					@GoInfoAddFoot="GoInfoAddFoot"
					:isLogin="isLogin">
				</pt-product-items>
			</view>
			<pt-loading v-if="isLoading"></pt-loading>
			<pt-nomore v-if="noMore && storeGoodsList.length"></pt-nomore>
			<pt-nothing 
				text="暂无商品" 
				icon="https://jiajudashi.oss-cn-shenzhen.aliyuncs.com/images/shop/icons/nothing.png?v=1" 
				v-if="noMore && !storeGoodsList.length" 
				bgColor="#FFF"
				:fixed="true"
				top="206"
				:isHigh="true">
			</pt-nothing>
		</block>
		<pt-loading></pt-loading>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				storeGoodsClassList: [],
				storeGoodsClassIndex: 0,
				storeGoodsList: [],
				class_id: '',
				noMore: false,
				page: 0,
				isLoading: false,
				isLogin: false
			};
		},
		onShow() {
			if(uni.getStorageSync('userInfo')){
				this.isLogin = true
			}else{
				this.isLogin = false
			}
		},
		onLoad(options) {
			if(options.parent_id){
				uni.setStorageSync('parent_id',options.parent_id)
				// 如果用户从分享的小程序卡片点击进来的,设置一个url缓存,登录后返回当前缓存url
				let pages = getCurrentPages();
				let curPage = pages[pages.length - 1];
				uni.setStorageSync('newurl',curPage.$page.fullPath)
				this.$u.api.binding_refer({id: options.parent_id}).catch(() => {
					console.log('接口报错：绑定失败')
					// uni.showToast({
					// 	icon: 'none',
					// 	title: '绑定失败'
					// })
				})
			}
			this.getMerchantClass()
			this.getMerchantPro()
		},
		methods: {
			getMerchantClass(){
				this.$u.api.StoreGoodsClassList({
					limit: 100,
					status: 1,
					type: 1,
					store_id: uni.getStorageSync('userInfo')?JSON.parse(uni.getStorageSync('userInfo')).store_id:1
				}).then(res => {
					this.storeGoodsClassList = res.datas
					this.storeGoodsClassList.unshift({
						class_name: '全部',
						class_id: ''
					})
				})
			},
			getMerchantPro(){
				this.$u.api.StoreGoodsList({
					type: 1,
					limit: 10,
					page: this.page,
					store_id: uni.getStorageSync('userInfo')?JSON.parse(uni.getStorageSync('userInfo')).store_id:1,
					class_id: this.class_id
				}).then(res => {
					let user_id = uni.getStorageSync('userInfo')?JSON.parse(uni.getStorageSync('userInfo')).user_id:0
					this.$u.mpShare = {
						title: '商品列表',
						path: '/merchant/store-pro/store-pro?parent_id=' + user_id
					}
					this.show = true
					if(res.datas.length){
						res.datas.forEach(item => {
							this.storeGoodsList.push(item)
						})
					}else{
						this.noMore = true
					}
				})
			},
			addFoot(where,detail,goods_id){
				this.$u.api.AddFoot({
					// #ifdef MP-WEIXIN
					type: 1,
					// #endif
					// #ifdef APP-PLUS
					type: 2,
					// #endif
					// #ifdef H5
					type: 3,
					// #endif
					store_id: uni.getStorageSync('userInfo')?JSON.parse(uni.getStorageSync('userInfo')).store_id:'',
					client: 1,
					where: where,
					stay_time: 0,
					goods_id: 0,
					store_goods_id: goods_id?goods_id:0,
					is_new: 1,
					detail: detail?detail:''
				})
			},
			storeGoodsClassTabChange(e){
				this.storeGoodsClassIndex = e
				this.class_id = this.storeGoodsClassList[e].class_id
				this.storeGoodsList = []
				this.noMore = false
				this.page = 0
				this.getMerchantPro()
			},
			search(){
				uni.navigateTo({
					url: '/pages/search/search',
					animationDuration: 500,
					animationType: 'slide-in-bottom'
				})
			},
			GoInfoAddFoot(goods_id){
				this.addFoot('自营商品-列表-详情','#客户查看了自营商品，建议给客户推荐相关热销商品',goods_id)
			}
		},
		onReachBottom() {
			if(!this.noMore){
				this.page++
				this.isLoading = true
				this.getMerchantPro()
			}
		}
	}
</script>

<style lang="scss">
	.store-pro{
		.store-pro-search{
			padding: 30rpx;
		}
		.store-pro-list{
			padding: 30rpx;
			/deep/ .product-items:last-child{
				border-bottom: none;
			}
		}
	}
</style>
